import { NestFactory, Reflector } from '@nestjs/core'
import { ClassSerializerInterceptor, ValidationPipe } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { UserService } from 'src/modules/user/user.service'
import { AppModule } from './app.module'
import { FormatResponseInterceptor } from './interceptor/format-response.interceptor'
import { InvokeRecordInterceptor } from './interceptor/invoke-record.interceptor'
import { CustomExceptionFilter } from './custom/exception.filter'
import { AuthInterceptor } from './interceptor/auth.interceptor'

async function bootstrap() {
  const app = await NestFactory.create(AppModule)
  const configService = app.get(ConfigService)

  app.useGlobalPipes(new ValidationPipe())
  // 不需要依赖注入
  app.useGlobalInterceptors(new FormatResponseInterceptor())
  app.useGlobalInterceptors(new InvokeRecordInterceptor())
  app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector))) // 序列化实体规则(例如去除password)
  app.useGlobalFilters(new CustomExceptionFilter())
  app.setGlobalPrefix(configService.get('nest_server_api_prefix'))

  await app.listen(configService.get('nest_server_port'), () => {
    console.log(`http://localhost:${configService.get('nest_server_port')}/${configService.get('nest_server_api_prefix')}`)
  })
}
bootstrap()
